﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using RapidWebDev.Platform;
using RapidWebDev.Common;

namespace RapidWebDev.Web
{
    public partial class RegisterVerify : System.Web.UI.Page
    {
        private static readonly IMembershipApi membershipApi = SpringContext.Current.GetObject<IMembershipApi>();
        private static readonly IOrganizationApi organizationApi = SpringContext.Current.GetObject<IOrganizationApi>();
       
        protected void Page_Load(object sender, EventArgs e)
        {
            string UserId = Request.QueryString["UserId"];
            //check the if the UserId exist
            if(string.IsNullOrEmpty(UserId))
                Response.Redirect("~/ErrorHandler.aspx?error=用户不能为空", true);
            UserObject user = membershipApi.Get(new Guid(UserId));
            if (user != null)//
            {
                if (((int)user["verifiedEmail"]) == 0)
                {
                    user.IsApproved = true;
                    user["verifiedEmail"] = 1;
                    membershipApi.Save(user, "Password1", "What's the name of your corp?");
                    //redirect to the user next register
                    Response.Redirect("~/Register.mvc?UserId="+UserId);
                }
                else
                {
                    Response.Redirect("~/ErrorHandler.aspx?error=这个用户 " + user.DisplayName + " 已经验证过邮件!",true);
                }
            }
        }
    }
}
